#include "smart_car.h"

using namespace std;

SmartCar::SmartCar() {
    _all_components.push_back(&agx);
    _all_components.push_back(&tire);
    _all_components.push_back(&chasis);
    _all_components.push_back(&camera);
    _all_components.push_back(&lidar);
    _all_components.push_back(&gyro);
    _all_components.push_back(&lcd);
    _all_components.push_back(&battery);
}

void SmartCar::operator=(const SmartCar& car) {
    _id = car._id;
    stu = car.stu;
    chasis = car.chasis;
    tire = car.tire;
    agx = car.agx;
    camera = car.camera;
    lidar = car.lidar;
    gyro = car.gyro;
    lcd = car.lcd;
    battery = car.battery;
}

void SmartCar::setID(std::string id) {
    this->_id = id;
}

std::string& SmartCar::getID() {
    return _id;
}

void SmartCar::print() {
    cout << "小车编号：" << this->_id << endl;
    cout << "学生姓名：" << stu.stuName << "  学生学号：" << stu.stuId << endl;
    // for (size_t i = 0; i < _all_components.size(); i++) {
    //     _all_components[i]->print();  // 这里为什么会有bug，调用第一个函数后，后面的函数都不会调用了
    // }
    chasis.print();
    tire.print();
    agx.print();
    camera.print();
    lidar.print();
    gyro.print();
    lcd.print();
    battery.print();
    return;
}

void SmartCar::save(std::string fName) {
    YAML::Node node;
    node["id"] = _id;
    node["Student"] = YAML::Node(YAML::NodeType::Map);
    node["Student"]["stuId"] = stu.stuId;
    node["Student"]["stuName"] = stu.stuName;
    for (auto com : _all_components) {
        com->save(node);
    }
    ofstream fout(fName);
    if (!fout.is_open()) {
        std::cerr << "!!!!!!无法打开文件进行写入!!!!!" << std::endl;
        return;
    }
    fout << node;
    fout.flush();
    fout.close();
    return;
}

int SmartCar::setStudent(Student& stu) {
    stu.stuId = stu.stuId;
    stu.stuName = stu.stuName;
    return 1;
}
